package com.tan.lbs.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;

import com.tan.lbs.dao.DesignerDAO;
import com.tan.lbs.entity.Designer;
import com.tan.lbs.util.HibernateUtil;

public class DesignerDAOImpl implements DesignerDAO {

	@Override
	public String create(Designer designer) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.save(designer);
		s.getTransaction().commit();
		return designer.getDesignerID();
	}

	@Override
	public Designer readByID(String designerID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Designer d = (Designer)s.get(Designer.class, designerID);
		s.getTransaction().commit();
		return d;
	}

	@Override
	public Designer readByUserName(String username) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		List<Designer> ds = s.createCriteria(Designer.class).add(Restrictions.
				eq("username", username)).list();
		s.getTransaction().commit();
		if(ds.size() ==0) return null;
		else {
			return (Designer)ds.get(0);
		}
	}

}
